package com.shensi.adminCollect.controller.customer;


import cn.hutool.core.date.DateUtil;
import com.shensi.adminCollect.common.annotation.Excel;
import com.shensi.adminCollect.common.constant.UserConstants;
import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.customer.CustomerCardRecordExportVO;
import com.shensi.adminCollect.domain.customer.CustomerCardUseRecord;
import com.shensi.adminCollect.enums.customer.CardRecordType;
import com.shensi.adminCollect.enums.customer.CardUseRangeEnum;
import com.shensi.adminCollect.enums.customer.ElectricCardUsageTypeEnum;
import com.shensi.adminCollect.enums.customer.ElectronCardType;
import com.shensi.adminCollect.service.customer.ICustomerCardUseRecordService;
import com.shensi.adminCollect.service.customer.ICustomerStationEnterprisedService;
import com.shensi.adminCollect.domain.system.SysAppUserInfoUser;
import com.shensi.adminCollect.domain.system.SysStationUser;
import com.shensi.adminCollect.service.system.IAppUserInfoUserService;
import com.shensi.adminCollect.service.system.ISysStationUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.common.core.domain.entity.SysUser;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户电子会员卡充值消费记录Controller
 *
 * @author qujia
 * @date 2021-05-18
 */
@RestController
@RequestMapping("/customer/record")
public class CustomerCardUseRecordController extends BaseController
{
    @Autowired
    private ICustomerCardUseRecordService customerCardUseRecordService;


    @Autowired
    private ISysStationUserService stationUserService;

    @Autowired
    private IAppUserInfoUserService appUserInfoUserService;

    @Autowired
    private ICustomerStationEnterprisedService customerStationEnterprisedService;




    /**
     * 查询客户电子会员卡充值消费记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerCardUseRecord form)
    {
        if(form.getMemberCode()!=null&&!form.getMemberCode().equals("")){
//            负责人建议写法。。。
            SysAppUserInfoUser userFormForMemberCode = new SysAppUserInfoUser();
            userFormForMemberCode.setMemberCode(form.getMemberCode());
            userFormForMemberCode.setStationId(form.getEnterpriseId());
            SysAppUserInfoUser  userForMemberCode = appUserInfoUserService.getOne(userFormForMemberCode);
            if(userForMemberCode!=null){
                form.setMobilePhone(userForMemberCode.getMobilePhone());
            }else{
                form.setMobilePhone("9999999999");
            }
        }
        startPage();
        return getDataTable(customerCardUseRecordService.selectCardUseRecordVoList(form));
    }

    /**
     * 查询客户电子会员卡充值消费记录列表
     */
    @GetMapping("/phonelist")
    public TableDataInfo phonelist(CustomerCardUseRecord customerCardUseRecord)
    {

        SysStationUser sysStationUser=new SysStationUser();
        customerCardUseRecord.setPayType("stk");
        sysStationUser.setEnterpriseId(customerCardUseRecord.getEnterpriseId());
        List<SysStationUser> list=stationUserService.selectSysStationUserList(sysStationUser);
        List<String> list1=new ArrayList<>();
        if(list!=null && list.size()>0){
            for(SysStationUser s:list) {
                list1.add(s.getStationId()+"");
            }
            customerCardUseRecord.setStationIdList(list1);
        }
        customerCardUseRecord.setStatus("1");
        startPage();
        List<CustomerCardUseRecord> recordList = customerCardUseRecordService.selectCustomerCardUseRecordList(customerCardUseRecord);
        return getDataTable(recordList);
    }

    /**
     * 查询客户充值/消费记录汇总
     */
    @GetMapping(value = "/listCZXFData")
    public AjaxResult listCZXFData(CustomerCardUseRecord customerCardUseRecord)
    {
//        List<String> list = sysDeptService.levelDeptList(customerCardUseRecord.getLevelId());
//        if(list!=null && list.size()>0){
//            customerCardUseRecord.setStationIdList(list);
//            customerCardUseRecord.setStationId(null);
//        }
        LoginUser current=SecurityUtils.getLoginUser();
        SysStationUser sysStationUser=new SysStationUser();
        sysStationUser.setEnterpriseId(current.getUser().getEnterpriseId());
        List<SysStationUser> list=stationUserService.selectSysStationUserList(sysStationUser);
        List<String> list1=new ArrayList<>();
        if(list!=null && list.size()>0){
            for(SysStationUser s:list) {
                list1.add(s.getStationId()+"");
            }
            customerCardUseRecord.setStationIdList(list1);
        }
        customerCardUseRecord.setStatus("1");
        return AjaxResult.success(customerCardUseRecordService.listCZXFData(customerCardUseRecord));
    }

    /**
     * 查询客户充值/消费记录汇总 (包括已经退款的)
     */
    @GetMapping(value = "/listAllCZXFData")
    public AjaxResult listAllCZXFData(CustomerCardUseRecord customerCardUseRecord)
    {
//        List<String> list = sysDeptService.levelDeptList(customerCardUseRecord.getLevelId());
//        if(list!=null && list.size()>0){
//            customerCardUseRecord.setStationIdList(list);
//            customerCardUseRecord.setStationId(null);
//        }
        LoginUser current=SecurityUtils.getLoginUser();
        SysStationUser sysStationUser=new SysStationUser();
        sysStationUser.setEnterpriseId(current.getUser().getEnterpriseId());
        List<SysStationUser> list=stationUserService.selectSysStationUserList(sysStationUser);
        List<String> list1=new ArrayList<>();
        if(list!=null && list.size()>0){
            for(SysStationUser s:list) {
                list1.add(s.getStationId()+"");
            }
            customerCardUseRecord.setStationIdList(list1);
        }
        customerCardUseRecord.setStatus("1");
        return AjaxResult.success(customerCardUseRecordService.listAllCZXFData(customerCardUseRecord));
    }
    /**
     * 导出客户电子会员卡充值消费记录列表
     */
    @GetMapping("/export")
    public AjaxResult export(CustomerCardUseRecord customerCardUseRecord) {
        List<CustomerCardUseRecord> records = customerCardUseRecordService.selectCardUseRecordVoList(customerCardUseRecord);
        List<CustomerCardRecordExportVO> customerCardRecordExportVOS = covertCustomerCardUseRecordList(records);
        ExcelUtil<CustomerCardRecordExportVO> util = new ExcelUtil<>(CustomerCardRecordExportVO.class);

        util.init(customerCardRecordExportVOS,"record", Excel.Type.EXPORT);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<Object[]> fields = util.getFields();
        if (StringUtils.equals(UserConstants.LEVEL_TWO,user.getUserLevel())) {
            //油企账号
            fields.remove(6);
            fields.remove(0);
        }else if(StringUtils.equals(UserConstants.LEVEL_THREE,user.getUserLevel())){
            //油站账号
            fields.remove(6);
            fields.remove(1);
            fields.remove(0);
        }
        return util.exportExcel();
    }

    private List<CustomerCardRecordExportVO> covertCustomerCardUseRecordList(List<CustomerCardUseRecord> list) {
        List<CustomerCardRecordExportVO> collect = list.stream().map(m -> {
            CustomerCardRecordExportVO customerCardRecordExportVO = new CustomerCardRecordExportVO();

            customerCardRecordExportVO.setEnterpriseName(m.getEnterpriseName());
            customerCardRecordExportVO.setStationName(m.getStationName());
            customerCardRecordExportVO.setCustomerName(m.getCustomerName());
            customerCardRecordExportVO.setCardUseRangeFlag(CardUseRangeEnum.getByKey(m.getCardUseRangeFlag(),CardUseRangeEnum.FOR_ONE).getName());
            customerCardRecordExportVO.setOrderNo(m.getOrderNo());
            ElectricCardUsageTypeEnum typeEnum = ElectricCardUsageTypeEnum.getByKey(m.getUsageType());
            customerCardRecordExportVO.setUsageType(typeEnum == null ? "" : typeEnum.getName());
            customerCardRecordExportVO.setPayType(CardRecordType.getByKey(m.getPayType(), CardRecordType.CONSUME).getName());
            customerCardRecordExportVO.setCardOilsType(ElectronCardType.getByKey(m.getCardOilsType()).getName());
            customerCardRecordExportVO.setAmt(m.getAmt());
            customerCardRecordExportVO.setPresentAmt(m.getPresentAmt());
            customerCardRecordExportVO.setBalance(m.getBalance());
            customerCardRecordExportVO.setMobilePhone(m.getMobilePhone());
            customerCardRecordExportVO.setCreateTime(DateUtil.formatDateTime(m.getCreateTime()));

            customerCardRecordExportVO.setPayableAmount(m.getPayableAmount());
            customerCardRecordExportVO.setPaidAmount(m.getPaidAmount());
            customerCardRecordExportVO.setDiscountSum(m.getDiscountSum());
            customerCardRecordExportVO.setOperatorName(m.getOperatorName());
            customerCardRecordExportVO.setReason(m.getReason());

            return customerCardRecordExportVO;
        }).collect(Collectors.toList());

        return collect;
    }


    /**
     * 获取客户电子会员卡充值消费记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerCardUseRecordService.selectCustomerCardUseRecordById(id));
    }

    /**
     * 新增客户电子会员卡充值消费记录
     */
    @PreAuthorize("@ss.hasPermi('customer:record:add')")
    @PostMapping
    public AjaxResult add(@RequestBody CustomerCardUseRecord customerCardUseRecord)
    {
        return toAjax(customerCardUseRecordService.insertCustomerCardUseRecord(customerCardUseRecord));
    }

    /**
     * 修改客户电子会员卡充值消费记录
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerCardUseRecord customerCardUseRecord)
    {
        return toAjax(customerCardUseRecordService.updateCustomerCardUseRecord(customerCardUseRecord));
    }

    /**
     * 删除客户电子会员卡充值消费记录
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerCardUseRecordService.deleteCustomerCardUseRecordByIds(ids));
    }
}
